package com.xyb.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xyb.entity.SendSmsEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SMSUtils {
    //短信签名
    private static String signName;

    @Value("${config.sms.signName}")
    private void setSignName(String signName) {
        SMSUtils.signName = signName;
    }

    /**
     * KEY SECRET
     */
    private static String accessKeyId;

    @Value("${config.sms.accessKeyId}")
    public void setAccessKey(String accessKeyId) {
        SMSUtils.accessKeyId = Base64Utils.base64Decode(accessKeyId);
    }

    private static String accessKeySecret;

    @Value("${config.sms.accessKeySecret}")
    public void setAccessKeySecret(String accessKeySecret) {
        SMSUtils.accessKeySecret = Base64Utils.base64Decode(accessKeySecret);
    }

    /**
     * 验证码
     */
    private static String regCode;

    @Value("${config.sms.templateCode.regCode}")
    public void setRegCode(String regCode) {
        SMSUtils.regCode = regCode;
    }

    private static String loginCode;

    @Value("${config.sms.templateCode.loginCode}")
    public void setLoginCode(String loginCode) {
        SMSUtils.loginCode = loginCode;
    }

    private static String resetPwdCode;

    @Value("${config.sms.templateCode.resetPwdCode}")
    public void setResetPwdCode(String resetPwdCode) {
        SMSUtils.resetPwdCode = resetPwdCode;
    }

    //阿里云 SMS 客户端
    public static Client createClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    /**
     * 发送注册验证码短信
     * 您正在申请手机注册，验证码为：${code}，5分钟内有效！
     */
    public static void sendRegCode(String iphoneNo, Integer code) throws Exception {
        Client client = SMSUtils.createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(iphoneNo)
                .setSignName(signName)
                .setTemplateCode(regCode)
                .setTemplateParam("{\"code\":" + code + "}");
        client.sendSms(sendSmsRequest);
    }

    /**
     * 发送登录验证码
     * 验证码为：${code}，您正在登录，若非本人操作，请勿泄露。
     */
    public static void sendLoginCode(String iphoneNo, Integer code) throws Exception {
        Client client = SMSUtils.createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(iphoneNo)
                .setSignName(signName)
                .setTemplateCode(loginCode)
                .setTemplateParam("{\"code\":" + code + "}");
        client.sendSms(sendSmsRequest);
    }

    /**
     * 发送重置密码短信
     * 您的验证码为：${code}，您正在进行密码重置操作，如非本人操作，请忽略本短信！
     */
    public static void sendResetCode(String iphoneNo, Integer code) throws Exception {
        Client client = SMSUtils.createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(iphoneNo)
                .setSignName(signName)
                .setTemplateCode(resetPwdCode)
                .setTemplateParam("{\"code\":" + code + "}");
        client.sendSms(sendSmsRequest);
    }

    public static void sendSms(SendSmsEntity entity) throws Exception {
        ObjectMapper om = new ObjectMapper();
        Client client = SMSUtils.createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(entity.getIphoneNo())
                .setSignName("".equals(entity.getSignName())?signName: entity.getSignName())
                .setTemplateCode(entity.getTemplateCode())
                .setTemplateParam(om.writeValueAsString(entity.getTempParam()));
        client.sendSms(sendSmsRequest);
    }
}
